package com.aier.user.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.user.entity.UserPhotoImg;

public class UserPhotoImgRowMapper implements RowMapper<UserPhotoImg> {

    @SuppressWarnings("unused")
    private int type;


    public UserPhotoImgRowMapper() {
    }

    public UserPhotoImgRowMapper(int type) {
        this.type = type;
    }

    @Override
    public UserPhotoImg mapRow(ResultSet rs, int rowNum) throws SQLException {

        UserPhotoImg u = parseUserPhotoImg(new UserPhotoImg(), rs);
        
        return u;
    }

    public static UserPhotoImg parseUserPhotoImg(UserPhotoImg u, ResultSet rs) throws SQLException {
        return parseUserPhotoImg(u, rs, null);
    }

    public static UserPhotoImg parseUserPhotoImg(UserPhotoImg u, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            u.setUpiid(rs.getLong(su + "upiid"));
        } catch (SQLException e) {
            u.setUpiid(null);
        }
        try {
            u.setUptid(rs.getLong(su + "uptid"));
        } catch (SQLException e) {
            u.setUptid(null);
        }
        try {
            u.setDesinfo(rs.getString(su + "desinfo"));
        } catch (SQLException e) {
            u.setDesinfo(null);
        }
        try {
            u.setImg(rs.getString(su + "img"));
        } catch (SQLException e) {
            u.setImg(null);
        }
       
        return u;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, UserPhotoImg u) {
        par.put("upiid", u.getUpiid());
        par.put("uptid", u.getUptid());
        par.put("desinfo", u.getDesinfo());
        par.put("img", u.getImg());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, UserPhotoImg u) {
        args.put("upiid", u.getUpiid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final UserPhotoImg u) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(u),
                        new String[] { "upiid" });
                parsePS(ps, u);
                return ps;
            }

        };
    }

    private static String parseSQL(UserPhotoImg u) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into user_photo_img(");
        int count = 0;
        if (null != u.getUptid()) {
            sql.append("uptid,");
            count++;
        }
        if (null != u.getDesinfo()) {
            sql.append("desinfo,");
            count++;
        }
        if (null != u.getImg()) {
            sql.append("img,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, UserPhotoImg u)
        throws SQLException {
        int count = 1;
        if (null != u.getUptid()) {
            ps.setLong(count++, u.getUptid());
        }
        if (null != u.getDesinfo()) {
            ps.setString(count++, u.getDesinfo());
        }
        if (null != u.getImg()) {
            ps.setString(count++, u.getImg());
        }
        

    }

}
